﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using KA41HLT;
using ReflectionStudio.Classes;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class KA41HLT : DocumentBase
    {
        #region==================Properties=====================
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        private string UserID
        {
            get
            {
                return txtUserID.Text.ToString();
            }
        }
        private string WAART
        {
            get
            {
                return cmbWAART.Text.ToString();
            }
        }
        private double fnratio
        {
            get
            {
                return (System.Convert.ToDouble(txtFNRatio.Text.ToString()));
            }
        }
        private SETKA41HLT src
        {
            get
            {
                return (SETKA41HLT)TryFindResource("dataSource");
            }
        }
        private SETKA41HLT setart = new SETKA41HLT();
        CollectionViewSource vwQUPROCESS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwQUPROCESS");
            }
        }
        DataRow dwQUPROCESS
        {
            get
            {
                if (vwQUPROCESS.View == null)
                    return null;
                if ((DataRowView)vwQUPROCESS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwQUPROCESS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwQUTMASTER
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwQUTMASTER");
            }
        }
        DataRow dwQUTMASTER
        {
            get
            {
                if (vwQUTMASTER.View == null)
                    return null;
                if ((DataRowView)vwQUTMASTER.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwQUTMASTER.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWASTKE
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWASTKE");
            }
        }
        DataRow dwWASTKE
        {
            get
            {
                if (vwWASTKE.View == null)
                    return null;
                if ((DataRowView)vwWASTKE.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWASTKE.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWASTAMM
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWASTAMM");
            }
        }
        DataRow dwWASTAMM
        {
            get
            {
                if (vwWASTAMM.View == null)
                    return null;
                if ((DataRowView)vwWASTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWAART
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWAART");
            }
        }
        DataRow dwWAART
        {
            get
            {
                if (vwWAART.View == null)
                    return null;
                if ((DataRowView)vwWAART.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWAART.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWAVAR
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWAVAR");
            }
        }
        DataRow dwWAVAR
        {
            get
            {
                if (vwWAVAR.View == null)
                    return null;
                if ((DataRowView)vwWAVAR.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWAVAR.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWACOLOR
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWACOLOR");
            }
        }
        DataRow dwWACOLOR
        {
            get
            {
                if (vwWACOLOR.View == null)
                    return null;
                if ((DataRowView)vwWACOLOR.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWACOLOR.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWAFINISH
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWAFINISH");
            }
        }
        DataRow dwWAFINISH
        {
            get
            {
                if (vwWAFINISH.View == null)
                    return null;
                if ((DataRowView)vwWAFINISH.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWAFINISH.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwKAAUF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwKAAUF");
            }
        }
        DataRow dwKAAUF
        {
            get
            {
                if (vwKAAUF.View == null)
                    return null;
                if ((DataRowView)vwKAAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwKAAUF.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWTGEW
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWTGEW");
            }
        }
        DataRow dwWTGEW
        {
            get
            {
                if (vwWTGEW.View == null)
                    return null;
                if ((DataRowView)vwWTGEW.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWTGEW.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWAFARBEN
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWAFARBEN");
            }
        }
        DataRow dwWAFARBEN
        {
            get
            {
                if (vwWAFARBEN.View == null)
                    return null;
                if ((DataRowView)vwWAFARBEN.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWAFARBEN.View.CurrentItem).Row;
            }
        }
        string fart
        {
            get
            {
                return m_Data.Left(txtQart.Text.ToString() + "                    ", 20);
            }
        }
        string fvar
        {
            get
            {
                return m_Data.Left(txtQvar.Text.ToString() + "  ", 2);
            }
        }
        string fcolor
        {
            get
            {
                return m_Data.Left(txtQColor.Text.ToString() + "            ", 10);
            }
        }
        string ffinish
        {
            get
            {
                return m_Data.Left(txtQFinish.Text.ToString() + "            ", 10);
            }
        }
        KA41HLTData m_Data, m_DataX;
        public DataSet setResult;
        private DataRow dwUI;
        KA41HLTReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        public DataSet prodcost = new DataSet();
        public DataSet rawyarncost = new DataSet();
        public KA41HLT()
        {
            InitializeComponent();
            this.Title = "报价系统";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new KA41HLTData(OracleDB, proxy);
                src.PRODCOST.Clear();
                src.YARNPRICE.Clear();
                prodcost = m_Data.LoadSetProdCost();
                rawyarncost = m_Data.LoadSetYarnPrice();
                src.Merge(prodcost);
                src.Merge(rawyarncost);

                this.txtQart.Text = "333650              ";
                this.txtQvar.Text = "A0";
                this.txtQColor.Text = "1         ";
                this.txtQFinish.Text = "Z00002    ";

                txtUserID.Text = "FPDE";
                txtPassword.Password = "FPDE";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string strCompany = dicParamater["Company"].ToString().Trim();
                string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
                DataSet SetUI = new DataSet();
                SetUI.Tables.Clear();
                SetUI.AcceptChanges();
                if (strCompany == "HLT")
                {
                    string Mandant = txtMANDANT.Text.ToUpper();

                    System.Security.SecureString SPassword = txtPassword.SecurePassword;
                    DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
                    foreach (DataRow dwTemp in setResult.Tables["HRSTAMM"].Rows)
                    {
                        txtSPRACHKZ.Text = dwTemp["HRSPRKZ"].ToString();
                        btnQuery.IsEnabled = true;
                        m_Data.UserID = UserID;
                        m_Data.MANDANT = Mandant;
                        m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void GenerateReport(string PrintType)
        {
            m_DataX = new KA41HLTData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt = new KA41HLTReport(dicParamater);
            m_rpt.dsSource = src;
            m_rpt.PrintType = PrintType;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport(PrintType);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    dwUI = setart.KAAUF.NewKAAUFRow();
                    dwUI["KAAART"] = fart;
                    dwUI["KAAVAR"] = fvar;
                    dwUI["KAAFARBE"] = fcolor;
                    dwUI["KAABESCHI"] = ffinish;
                    DataSet setresult = m_Data.GetData(dwUI);
                    src.Clear();
                    src.Merge(setresult);
                    src.Merge(prodcost);
                    src.Merge(rawyarncost);
                    vwWASTAMM.View.MoveCurrentToFirst();
                    vwWTGEW.View.MoveCurrentToFirst();
                    vwWASTKE.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        #region===============UI========= =================
        private void cmbWAART_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                string qart = txtQART.Text.ToString().Trim();
                if (dwWAART != null)
                {
                    int leng = qart.Length;
                    if (m_Data.Left(dwWAART["WAART"].ToString(), leng) == qart)
                        return;
                }
                setart.Clear();
                DataSet setResult = m_Data.LoadWAART(UserID, qart);
                setart.Merge(setResult);
                vwWAART.Source = setart.Tables["WAART"];
                vwWAART.View.MoveCurrentToFirst();
                cmbWAART.Text = txtQART.Text;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void cmbWAVAR_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DataSet setResult = new DataSet();
            try
            {
                var query = from dwVAR in setart.Tables["WAART"].AsEnumerable()
                            where dwVAR["WAART"].ToString() == dwWAART["WAART"].ToString()
                            orderby dwVAR["WAVAR"].ToString()
                            select dwVAR;
                vwWAVAR.Source = query.CopyToDataTable();
                vwWAVAR.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void cmbWACOLOR_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DataSet setResult = new DataSet();
            try
            {
                if (setart.WAFARBEN.Rows.Count > 0)
                {
                    if (setart.WAFARBEN.Rows[0]["WAFART"].ToString() == dwWAART["WAART"].ToString())
                        return;
                }
                setart.WAFARBEN.Clear();
                dwWAART["WAVAR"] = dwWAVAR["WAVAR"];
                setart.Merge(m_Data.LoadWAFARBEN(dwWAART));
                var query = from dwQuery in setart.WAFARBEN.AsEnumerable()
                            where dwQuery["WAFZUART"].ToString() == "C"
                            orderby dwQuery["WAFZUSATZ"].ToString()
                            select dwQuery;
                vwWACOLOR.Source = query.CopyToDataTable();
                vwWACOLOR.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void cmbWAFINISH_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DataSet setResult = new DataSet();
            try
            {
                var query = from dwQuery in setart.WAFARBEN.AsEnumerable()
                            where dwQuery["WAFZUART"].ToString() == "B"
                            orderby dwQuery["WAFZUSATZ"].ToString()
                            select dwQuery;
                vwWAFINISH.Source = query.CopyToDataTable();
                vwWAFINISH.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

        }
        private void btnCompute_Click(object sender, RoutedEventArgs e)
        {

        }
        private void cmdListQuotation_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                src.Merge(m_Data.ListQuatation(dwUI));
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void btnExProcess_Click(object sender, RoutedEventArgs e)
        {
            //SETUPEXPROC setup = new SETUPEXPROC();
            //setup.SetUp(dicParamater);
            //setup.Show();
            Type docType = typeof(SETUPEXPROC);
            DocumentFactory.Instance.OpenDocument(docType, new XpsDocumentDataContext(), dicParamater);
        }

        private void btnYarnPrice_Click(object sender, RoutedEventArgs e)
        {
            Type docType = typeof(SETUPRAWYARN);
            DocumentFactory.Instance.OpenDocument(docType, new XpsDocumentDataContext(), dicParamater);
            return;
            SETUPRAWYARN setup = new SETUPRAWYARN();
            setup.SetUp(dicParamater);
            setup.Show();
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            m_Data.ReSetSETUP("HLT", "");
            src.PRODCOST.Clear();
            src.YARNPRICE.Clear();
            prodcost = m_Data.LoadSetProdCost();
            rawyarncost = m_Data.LoadSetYarnPrice();
            src.Merge(prodcost);
            src.Merge(rawyarncost);

        }
    }
}
